package com.yeban.javase.day19;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class Test {

    public static void main(String[] args) {
        Socket socket = null;
        PrintWriter out = null;
        try {
            // 连接服务端
            socket = new Socket("192.168.0.189", 10086);

            // true 表示自动刷新
            out = new PrintWriter(socket.getOutputStream(), true);

            // 向服务器发送数据
            out.println("你好！我是小孙~");
            System.out.println("消息已发送到服务器。");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (out != null) out.close();
            if (socket != null) try { socket.close(); } catch (IOException ignored) {}
        }
    }
}
